
#Rem
Header: AGFX Font. 
#End

Import mojo
Import agfx

#Rem
summary: Max character in list. 
#End
Const MAX_CHARS:Int = 255

#Rem
summary: font information (ini file)
#End
Class TFontInfo
     
	  Field A:Int
	  Field C:Int
      Field Wid:Int
	  Field Hgt:Int
      Field char:String
      Field x1:Float
	  Field y1:Float
	  Field x2:Float
	  Field y2:Float
	  
	  Field ix1:Int
	  Field iy1:Int
	  Field ix2:Int
	  Field iy2:Int
	  Field iw:Int
	  Field ih:Int
	  
End Class

#Rem
summary: - TFont
#End
Class TFont
	
	#Rem
	summary: - fonts info list
	#End
	Field F:TFontInfo[MAX_CHARS]
	#Rem
	summary: lookup table for fonts
	#End
	Field CharLookup:Int[MAX_CHARS * 4]
	Field NUMFONTS:Int
	#Rem
	summary: font image
	#End
	Field fontAtlas:TImage	
	Field SpaceWidth:Int	
	Field MaxHeight:Int	
	Field ini_lines:StringList
		
	' CONSTRUCTOR:
	' ****************************************************************************
	#Rem
	summary: constructor
	#End
	Method New( )
		Self.NUMFONTS = 0
		Self.ini_lines = New StringList
	End Method
	
	
	' ****************************************************************************
	#Rem
	summary: Load and create font
	name:String font filename (no extension)
	#End
	Method Create:Void( name:String)
		Self.LoadFontTexture(name + ".png")
		Self.LoadFontIni(name + ".txt")
	End Method
	
	' HELP FUNCTION:
	' ****************************************************************************
	Method LoadFontTexture:Void(fonttexturefile:String)
		
		Self.fontAtlas = IMAGES.Load(fonttexturefile,,False,False)		
		AssertNotNull(Self.fontAtlas,"Missing image file = '"+fonttexturefile+"'")
		
	End Method
	
	' HELP FUNCTION:
	' ****************************************************************************
	Method GetIniLineByKey:String(keyword:String)
		Local id:Int
		Local Line:String
		For Line = eachin Self.ini_lines
		
	
			If Line.Find(keyword, 0) >= 0 Then
			    Local spl:String[] = Line.Split("=")
				Return spl[1]
			End If
		Next
	End Method
	
	' HELP FUNCTION:
	' ****************************************************************************
	Method LoadFontIni:Void(fontinifile:String)
		'Print(fontinifile)
		Local numfont:Int
		Local chidx:Int
		Local file:String = LoadString("graphics/"+fontinifile)
		file.Replace("~l","")
		Local lines := file.Split("~n")
		Local line:String
		
		For line = Eachin lines
			line = line.Replace(",",".")
			Self.ini_lines.AddLast(line)
			
		Next
			' prepare INI file
			
			Self.NUMFONTS = Int(GetIniLineByKey("NumFont"))
			Self.SpaceWidth = Int(GetIniLineByKey("SpaceWidth"))
			
			For chidx = 0 To Self.NUMFONTS - 1
			
				Self.F[chidx] = New TFontInfo
				
				Self.F[chidx].char = Self.GetIniLineByKey(chidx + "Char=")
				
				Self.CharLookup[Int(Self.F[chidx].char)] = chidx
				Self.F[chidx].A = Int(Self.GetIniLineByKey(chidx + "A="))
				
				Self.F[chidx].C = Int(Self.GetIniLineByKey(chidx + "C="))
				Self.F[chidx].Wid = Int(Self.GetIniLineByKey(chidx + "Wid="))
				Self.F[chidx].Hgt = Int(Self.GetIniLineByKey(chidx + "Hgt="))
				
				Self.F[chidx].x1 = Float(Self.GetIniLineByKey(chidx + "X1="))
				Self.F[chidx].y1 = Float(Self.GetIniLineByKey(chidx + "Y1="))
				Self.F[chidx].x2 = Float(Self.GetIniLineByKey(chidx + "X2="))
				Self.F[chidx].y2 = Float(Self.GetIniLineByKey(chidx + "Y2="))
				
				Self.F[chidx].ix1 = Self.F[chidx].x1 * Self.fontAtlas.image.Width()
				Self.F[chidx].iy1 = Self.F[chidx].y1 * Self.fontAtlas.image.Height()
				Self.F[chidx].ix2 = Self.F[chidx].x2 * Self.fontAtlas.image.Width()
				Self.F[chidx].iy2 = Self.F[chidx].y2 * Self.fontAtlas.image.Height()
				
				Local x:Int = Self.F[chidx].ix1 
				Local y:Int = Self.F[chidx].iy1 
				Local w:Int = Self.F[chidx].ix2 - x
				Local h:Int = Self.F[chidx].iy2 - y
				
				Self.F[chidx].iw = w
				Self.F[chidx].ih = h

				'Print(chidx+" "+x+" "+y+" "+w+" "+h)
				'Self.F[chidx].img = Self.TexInd.GrabImage(x,y,w,h,1)
			Next
			
	End Method
	
	
	' ****************************************************************************
	#Rem
	summary: - get text length
	txt:String - text
	#End
	Method TextLen:Int(txt:String)
		Local CurX:Int
		Local Ch:String
		Local Chaar:Int
		Local I:Int
		Local Ind:Int
		
		
		CurX = 0
		
		For I = 0 To txt.Length - 1
			
			Ch = txt[I]
			Chaar = Int(Ch)
			
			If Chaar = 32 Then
				Ind = -1
			    CurX = CurX + SpaceWidth
			Else
				Ind = CharLookup[Chaar]
			End If
			
			If ind > - 1 Then
				CurX = CurX + F[Ind].A
			    CurX = CurX + F[Ind].C
			End If
		Next

   		Return CurX
		
	End Method
	
	#Rem
	summary: Draw text
	Txt:String		- text
	X:Float			- x position
	Y:Float			- y position
	angle:Float=0 	- rotate
	#End
	Method DrawText:Void(Txt:String,X:Float, Y:Float, angle:Float=0 )
		PushMatrix()
			Translate X,Y
			Rotate angle
			Self._DrawText(Txt,0,0)			
		PopMatrix()
	End Method


	' CLASS FUNCTION:
	' ****************************************************************************
		
	Private	
	Method _DrawText:Void(Txt:String,X:Float, Y:Float )
		Local curX:Float
		Local ch:String
		Local chaar:Int
		Local i:Int
		Local ind:Int
	
		curX = X
	
		
		
	   	For i = 0 To Txt.Length - 1
			ch = Txt[i]

			chaar = Int(ch)
			
	       	If chaar = 32 Then
		        	ind = -1
		         	curX = curX + SpaceWidth
	       	Else
	         		ind = CharLookup[chaar]
			End If
	
	       	If ind > - 1 Then

	         	curX = curX + F[ind].A
		
				Local x:Int = Self.F[ind].ix1 
				Local y:Int = Self.F[ind].iy1 
				Local w:Int = Self.F[ind].iw
				Local h:Int = Self.F[ind].ih
				
				Self.fontAtlas.DrawSubImage(curX,Y,x,y,w,h)
				
				curX = curX + F[ind].C

			End If
	   	Next
	
	End Method
	
End Class


